package com.frontalcode 
{
	import fl.video.*;
	import flash.display.*;
	import flash.geom.*;

	use namespace flvplayback_internal;
	
	internal class FrontalFLVPlaybackUIMgr extends UIManager
	{
		flvplayback_internal var container : ContainerVideo;
		flvplayback_internal var controlAttacher : Container;
		flvplayback_internal var disabledControls : Object = { };
		
		// CONFIG::FLASH_CS3
		// {
		// 		flvplayback_var container : ContainerVideo;
		// 		flvplayback_var controlAttacher : Container;
		// }
		
		function FrontalFLVPlaybackUIMgr ( video : FLVPlayback, container : ContainerVideo )
		{
			super ( video );
			this.container = container;
		}
		
		override flvplayback_internal function layoutSkin ( ) : void
		{
			// From UIManager.
			//
			if ( layout_mc == null ) return;
			if ( skinLoadDelayCount < 2 ) return;
			
			var attacher : Container;
			if ( container.getStyle ( "control-attacher-index" ) != null ) attacher = container.getContainerByIndex ( container.getStyle ( "control-attacher-index" ) );
			
			if ( controlAttacher !== attacher )
			{
				if ( controlAttacher != null ) controlAttacher.removeEventListener ( FrontalEvent.RESIZE, controlAttacherResizeHandler );
				controlAttacher = attacher;
				if ( controlAttacher != null ) controlAttacher.addEventListener ( FrontalEvent.RESIZE, controlAttacherResizeHandler );
			}
			
			if ( ! _fullScreen && attacher != null )
			{
				var video_mc:DisplayObject = layout_mc [ "video_mc" ];
				placeholderLeft = video_mc.x;
				placeholderRight = video_mc.x + video_mc.width;
				placeholderTop = video_mc.y;
				placeholderBottom = video_mc.y + video_mc.height;

				videoBottom = videoTop = videoLeft = 0;
				videoRight = attacher.width;
				
				for ( var i : uint = 0; i < customClips.length; i++ ) 
				{
					layoutControl ( customClips [ i ] );
					if ( customClips [ i ] == border_mc ) bitmapCopyBorder ( );
				}
				for ( i = 0; i < NUM_CONTROLS; i++ ) layoutControl ( controls [ i ] );
			}
			else
			{
				super.layoutSkin ( );
			}
		}
		
		internal function controlAttacherResizeHandler ( event : FrontalEvent ) : void
		{
			layoutSkin ( );
		}
		
		override flvplayback_internal function enterFullScreenTakeOver ( ) : void 
		{
			if ( ! _fullScreen || cacheFLVPlaybackParent != null ) return;
			
			// If our skin is not attached to the video MovieClip then we will
			// not see it so attach it to the video MovieClip if necessary.
			//
			if ( skin_mc != null && skin_mc.parent != _vc )
			{
				if ( skin_mc.parent != null ) skin_mc.parent.removeChild ( skin_mc );
				( _vc as FrontalFLVPlayback ).addChildRaw ( skin_mc );
			}
			
			super.enterFullScreenTakeOver ( );
		}
		
		override flvplayback_internal function exitFullScreenTakeOver ( ) : void 
		{
			if ( cacheFLVPlaybackParent == null ) return;
			
			// Reattach our skin as necessary.
			//
			if ( container.getStyle ( "control-attacher-index" ) != null && skin_mc != null )
			{
				if ( skin_mc.parent != null ) skin_mc.parent.removeChild ( skin_mc );
				_vc.addChild ( skin_mc );
			}
			
			super.exitFullScreenTakeOver ( );
		}
		
		flvplayback_internal function setControlEnabled ( index : int, value : Boolean ) : void
		{
			disabledControls [ index ] = ! value;
		}
		
		override flvplayback_internal function dispatchMessage ( index : int ) : void 
		{
			if ( disabledControls [ index ] ) return;
			super.dispatchMessage ( index );
		}
	}
}


